﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://wpfimageviewer.codeplex.com
using System;
using System.Threading;
using BrainTechLLC;
using ImageAppViewModels;
using System.Windows.Threading;
using ImageAppDataModels;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows;
using System.Windows.Media;
using System.Drawing;

namespace WpfImageDuplicates
{
    public partial class ThumbnailView : UserControl
    {
        public static SolidColorBrush BlackBrush = new SolidColorBrush(Colors.Black);
        public static SolidColorBrush OrangeBrush = new SolidColorBrush(Colors.Orange);
        public static SolidColorBrush WhiteBrush = new SolidColorBrush(Colors.White);
        public static SolidColorBrush YellowBrush = new SolidColorBrush(Colors.Yellow);

        public static readonly DependencyProperty FileImageViewModelProperty =
            DependencyProperty.Register("FileImageViewModel", typeof(FileImageViewModel), typeof(ThumbnailView), new PropertyMetadata(null, OnFileImageViewModelPropertyChanged));

        public FileImageViewModel FileImageViewModel
        {
            get { return (FileImageViewModel)GetValue(FileImageViewModelProperty); }
            set { _vm = value; SetValue(FileImageViewModelProperty, value); }
        }

        private static void OnFileImageViewModelPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ThumbnailView source = d as ThumbnailView;
            source.UpdateFileImageViewModel(e.OldValue as FileImageViewModel, e.NewValue as FileImageViewModel);
        }

        private void UpdateFileImageViewModel(FileImageViewModel viewModelOld, FileImageViewModel viewModelNew)
        {
            FileImageViewModel = viewModelNew;

            if (viewModelNew != viewModelOld)
            {
                if (viewModelOld != null)
                {
                    viewModelOld.Visible = false;
                    viewModelOld.SetThumbEvent -= _vm_SetThumbEvent;
                    viewModelOld.PropertyChanged -= viewModelNew_PropertyChanged;
                }

                // Let the UI bind to the view-model.
                this.DataContext = viewModelNew;

                commandBar.DataContext = (viewModelNew == null) ? null : Settings.Values;
                commandBar.FileImageViewModel = viewModelNew;

                if (viewModelNew != null)
                {
                    viewModelNew.Dirty = true;
                    viewModelNew.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(viewModelNew_PropertyChanged);
                }
            }
        }

        void viewModelNew_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "SelectedByUser")
            {
                IsSelected = FileImageViewModel.SelectedByUser;
                ItemSelectedByUser.RaiseEvent(this, FileImageViewModel.SelectedByUser);
            }
            else if (e.PropertyName == "DeselectedByUser")
            {
                ItemDeselectedByUser.RaiseEvent(this, FileImageViewModel.DeselectedByUser);
            }
            else if (e.PropertyName == "IsSelected")
            {
                IsSelected = FileImageViewModel.IsSelected;
            }
            else if (e.PropertyName == "DisplayingGroups")
            {
                UpdateDisplayBasedOnDisplayingGroups();
            }            
        }

        public void UpdateDisplayBasedOnDisplayingGroups()
        {
            if (_vm == null || !_vm.ThumbVisible)
                return;

            if (string.IsNullOrEmpty(_vm.DisplayingGroups))
            {
                this.BorderBrush = BlackBrush;
                this.Background = WhiteBrush;
            }
            else
            {
                if (this.BorderBrush != OrangeBrush)
                {
                    this.BorderBrush = OrangeBrush;
                    this.Background = YellowBrush;
                }
            }
        }

        void _vm_SetThumbEvent(object sender, ObjectEventArgs<FileImage> e)
        {
            if (_vm == null || imgMain == null)
                return;

            ImageSource cached = _vm.ThumbImageSourceCachedDirty ? null : _vm.ThumbImageSourceCached;

            if (cached == null)
            {
                Bitmap bm = _vm.Item.ThumbnailCached;

                if (bm != null)
                {
                    SetThumb(bm);
                }
                else
                {
                    var thumb = _vm.GetThumbnail(LoadOptions.LoadAsync, (fi, t) => SetThumb(fi), true);

                    if (thumb != null)
                    {
                        SetThumb(thumb.ThumbnailCached);
                    }
                }
            }

            SetThumbImage.RaiseEvent(this);
        }
    }
}
